package utilities;

import PCR.MainFrame;
import components.Band;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.Vector;
import javax.swing.JOptionPane;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.CombinedDomainCategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.ApplicationFrame;

public class DualAxisChart extends ApplicationFrame {

    final String team1 = "1st Team";
    final String team2 = "2nd Team";
    Vector<Integer> BandBrightness = new Vector<Integer>();
    Vector<Integer> BandBrightDivision = new Vector<Integer>();

    public DualAxisChart(String titel) {
        super(titel);
        MainFrame frm = (MainFrame) java.awt.Window.getWindows()[0];
        for (Component com : frm.getSelectedWorkArea().getBandsPanel().getComponents()) {
            if (com instanceof Band) {
                Band curBand = (Band) com;
                BandBrightness.add(curBand.getBrightness());
            }
        }
        if (BandBrightness.size() % 2 == 0) {
            
            int i = 0;
            while (i < BandBrightness.size())
            {
                BandBrightDivision.add(BandBrightness.get(i)/BandBrightness.get(i+1));
                i+=2;
            }
            
            JFreeChart chart = createChart();
            ChartPanel chartPanel = new ChartPanel(chart);
            chartPanel.setPreferredSize(new java.awt.Dimension(600, 450));
            setContentPane(chartPanel);
        } else {
            JOptionPane.showMessageDialog(null, "Missing Band or House Keeping");
        }
    }

    public double[] run() {
        double[] run = new double[BandBrightDivision.size()];
        for(int i=0;i<BandBrightDivision.size();i++)
        {
            run[i] = BandBrightDivision.get(i);
        }
        return run;
    }

    private CategoryDataset createRunDataset1() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        double[] run = run();
        for (int i = 0; i < run.length; i++) {
            dataset.addValue(run[i], team1 + " Run", "" + (i + 1));
        }
        return dataset;
    }

    private JFreeChart createChart() {

        final CategoryDataset dataset1 = createRunDataset1();
        final NumberAxis rangeAxis1 = new NumberAxis("Run");
        rangeAxis1.setStandardTickUnits(
                NumberAxis.createIntegerTickUnits());
        final BarRenderer renderer1 = new BarRenderer();
        renderer1.setSeriesPaint(0, Color.red);
        renderer1.setBaseToolTipGenerator(
                new StandardCategoryToolTipGenerator());
        final CategoryPlot subplot1 = new CategoryPlot(dataset1, null, rangeAxis1, renderer1);
        subplot1.setDomainGridlinesVisible(true);

        final ValueAxis axis2 = new NumberAxis("Run Rate");
        subplot1.setRangeAxis(1, axis2);
        subplot1.mapDatasetToRangeAxis(1, 1);
        final CategoryItemRenderer runrateRenderer1 = new LineAndShapeRenderer();
        runrateRenderer1.setSeriesPaint(0, Color.red);
        subplot1.setForegroundAlpha(0.7f);
        subplot1.setRenderer(0, renderer1);
        subplot1.setRenderer(1, runrateRenderer1);


        final NumberAxis rangeAxis2 = new NumberAxis("Run");
        rangeAxis2.setStandardTickUnits(
                NumberAxis.createIntegerTickUnits());
        final BarRenderer renderer2 = new BarRenderer();
        renderer2.setSeriesPaint(0, Color.blue);
        renderer2.setBaseToolTipGenerator(
                new StandardCategoryToolTipGenerator());
        final CategoryAxis domainAxis = new CategoryAxis("Over");
        final CombinedDomainCategoryPlot plot =
                new CombinedDomainCategoryPlot(domainAxis);

        plot.add(subplot1, 1);
        final JFreeChart chart = new JFreeChart(
                "Score Bord", new Font("SansSerif", Font.BOLD, 12),
                plot, true);
        return chart;
    }
}